package com.blast.comicsisland.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.blast.comicsisland.R;
import com.blast.comicsisland.adapters.RankItemAdapter;
import com.blast.comicsisland.model.recommend.recommendbody.ComicsList;
import com.blast.comicsisland.model.recommend.recommendbody.Info;
import com.blast.comicsisland.utils.JsonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class RankItemActivity extends ActionBarActivity implements View.OnClickListener, AbsListView.OnScrollListener, AdapterView.OnItemClickListener {

    private TextView textView;
    private ListView listView;
    private int pageNo = 1;
    private int specialid;
    private RankItemAsyncTask rankItemAsyncTask;
    private Info info;
    private List<ComicsList> comicsList;
    private RankItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rank_item);

        textView = (TextView) findViewById(R.id.rank_item_title_text);
        ImageView imageView = (ImageView) findViewById(R.id.rank_item_back_image);
        imageView.setOnClickListener(this);
        listView = (ListView) findViewById(R.id.rank_item_list_view);
        listView.setOnScrollListener(this);
        Intent intent = getIntent();
        String name = intent.getStringExtra("name");
        textView.setText(name);
        specialid = intent.getIntExtra("specialid", 0);

        listView.setOnItemClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();

        String path = "http://mhjk.1391.com/comic/comicslist_v2?e=" + (System.currentTimeMillis() / 1000);
        //{“pageno”:”1","pagesize":"10","special":"32","sort":"4"}
        String jsonPath =
                "{\"pageno\":\"" + pageNo + "\",\"pagesize\":\"10\",\"special\":\"" + specialid + "\",\"sort\":\"4\"}";


        rankItemAsyncTask = new RankItemAsyncTask();

        rankItemAsyncTask.execute(path, jsonPath);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    //---------------------------------->
    //监听listView滑动到底部的实际那
    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
                if (view.getLastVisiblePosition() == view.getCount() - 1) {
                    pageNo++;
                    String path = "http://mhjk.1391.com/comic/comicslist_v2?e=" + (System.currentTimeMillis() / 1000);
                    //{“pageno”:”1","pagesize":"10","special":"32","sort":"4"}
                    String jsonPath =
                            "{\"pageno\":\"" + pageNo + "\",\"pagesize\":\"10\",\"special\":\"" + specialid + "\",\"sort\":\"4\"}";

                    rankItemAsyncTask = new RankItemAsyncTask();
                    rankItemAsyncTask.execute(path, jsonPath);
                }
                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }
    //-------------------------------------->

    //listView点击事件
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(this,CommicActivity.class);
        if (comicsList != null) {
            ComicsList comicsList1 = comicsList.get(position);
            String bigBookId = comicsList1.getBigbookId();
            intent.putExtra("bigBookId",bigBookId);
            startActivity(intent);
        }

    }
    //------------------------------------->


    //异步任务
    class RankItemAsyncTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            String ret = null;
            String path = params[0];
            String jsonPath = params[1];
            if (path != null && jsonPath != null) {
                ret = JsonUtils.doPostRequest(path, jsonPath);
            }
            return ret;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            if (s != null) {
                try {
                    JSONObject object = new JSONObject(s);
                    if (info == null) {
                        info = new Info();
                    }
                    JSONObject infoObject = object.getJSONObject("info");
                    info.jsonParse(infoObject);
                    comicsList = info.getComicsList();
                    if (comicsList != null) {
                        if (adapter == null) {
                            adapter = new RankItemAdapter(RankItemActivity.this, comicsList);
                            listView.setAdapter(adapter);
                        }
                        adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}
